﻿using System;
using System.Linq;
using System.Collections.Generic; 
using NTTDATA.BEENChina.ResultCertification.Config; 

namespace NTTDATA.BEENChina.ResultCertification.Checker.Name
{
   
    [Serializable]
    [VerfityAttribute(Id = "VerfityN0201", Name = "补足检证1", CategoryType = EnumCategoryType.姓名, Model = true)]
    public class VerfityN0201 : VerfityOneResult
    { 

        public override bool CheckResult(List<List<string>> result, string entryCommon)
        {
            bool bresult = false;
            if (result == null)
            {
                return false;
            }

            if (entryCommon.Equals("30"))
            {
                return false;
            }

            foreach (var rowResult in result)
            {
                foreach (string colResult in rowResult)
                {
                    var splitEntryResult =new Split.SplitEntryResult(
                        ChildImageObject.CategorySetting.DataFormat, colResult);

                    string kanaResult = splitEntryResult.GetNameOfKana();
                     kanaResult = Util.JaStringUtil.ChangeJaInputToHirakana(kanaResult);
                    

                    string kannjiResult = splitEntryResult.GetNameOfKannji();
                    kannjiResult = Util.JaStringUtil.ChangeJaInputToHirakana(kannjiResult);

                    bresult = CheckErrorNameResult(kanaResult, kannjiResult);

                    if (bresult) break;
                }
                if (bresult) break;
            }
      

            if (bresult)
            {
                System.Console.WriteLine("VerfityN0201");
            }

            return bresult;
        }

        /// <summary>
        /// 假名部分有读音
        /// 汉字部分如果有假名，假名部分全部出现在假名里 ok
        /// 汉字部分没有假名，全是汉字 略过
        /// </summary>
        /// <param name="kanaResult"></param>
        /// <param name="kannjiResult"></param>
        /// <returns></returns>
        protected bool CheckErrorNameResult(string kanaResult ,   string kannjiResult)
        {
            bool bresult = false;

          /* if (!String.IsNullOrEmpty( kannjiResult ))
           {
              // var kannjiSplit = kannjiResult.Split(new string[]{string.Empty},StringSplitOptions.None);
               var kannjiSplit = Split.SplitNameResult.GetSplitResult(kannjiResult);

               if (kannjiSplit.Count.Equals(2))
               {
                   //汉字第一部分是汉字  第二部分非汉字情况下
                   if (IsKannji(kannjiSplit[0]) && !IsKannji(kannjiSplit[1]))
                   {
                       if (!String.IsNullOrEmpty(kanaResult))
                       {
                          // var kanaSplit = kanaResult.Split(new string[] { string.Empty }, StringSplitOptions.None);

                           var kanaSplit = Split.SplitNameResult.GetSplitResult(kanaResult);

                            if (kanaSplit.Count.Equals(2))
                            {
                                //假名第一部分是假名，第二部分如果是空白
                                if (IsKana(kanaSplit[0]) && string.IsNullOrEmpty(kanaSplit[1]))
                                {
                                    bresult = true;
                                }
                            }
                       }
                   }
               }
           }*/

            if (!string.IsNullOrEmpty(kanaResult))
            {
                
                var kanaSplit = Split.SplitNameResult.GetSplitResult(kanaResult);

                if (kannjiResult != null)
                {
                    var kannjiSplit = Split.SplitNameResult.GetSplitResult(kannjiResult);
               
                    for (int i = 0; i < kannjiSplit.Count; i++)
                    { 

                        if ( i<kannjiSplit.Count && kannjiSplit.Count == kanaSplit.Count)
                        {
                            //汉字部分和假名部分 分割成同样数量
                            string kannjiCell = kannjiSplit[i];
                            string kanaCell = string.Empty;
                        
                            if ( i < kanaSplit.Count )
                                  kanaCell= kanaSplit[i];

                            //漢字部分のその他　文字
                            var kannjiOtherCharacters = GetOtherCharactersFromKannji(kannjiCell);

                            if (kannjiOtherCharacters.Any())
                            {
                                
                                bresult = kannjiOtherCharacters.Any(t => !kanaCell.Contains(t));

                                if (bresult)
                                    break;
                            }
                            
                        }
                        else if ( i<kannjiSplit.Count )
                        {
                            //汉字部分和假名部分 分割成不同数量
                            //
                            string kannjiCell = kannjiSplit[i];


                            //漢字部分のその他　文字
                            var kannjiOtherCharacters = GetOtherCharactersFromKannji(kannjiCell); ;

                            //汉字里带假名
                            if (kannjiCell.Length != kannjiOtherCharacters.Count)
                            {
                                continue;
                            }
                            //汉字里不带假名
                            else
                            {
                                if (kannjiOtherCharacters.Any())
                                {
                                    bresult = kannjiOtherCharacters.Any(t => !kanaResult.Contains(t));

                                    if (bresult)
                                        break;
                                }
                            }
                           
                        }
                    }
                }
            }
            return bresult;
        }

        private static List<char> GetOtherCharactersFromKannji(string kannjiCell)
        {
            char[] otherSameChar = {'々','ヶ'};
        
            var kannjiOtherCharacters = kannjiCell.ToCharArray().Where(t => !Util.StrHelper.IsKannji(t)).ToList();


            return kannjiOtherCharacters.Where(t => !t.Equals('?') && !otherSameChar.Contains(t)).Where(t => !Util.StrHelper.IsExternalChar(t)).ToList();
        }

        private bool IsKannji(string result)
        {
            return Util.StrHelper.IsKannji(result);
        }
        private bool IsKana(string result)
        {
            return Util.StrHelper.IsKana(result); ;
        }
    }
}
